$(function () {
    var $ai_setting_success_div = $("#ai-setting-success-div");
    var $ai_setting_regist_div = $("#ai-setting-regist-div");
    var access_token = "ff3ac757-4b53-4651-aef8-21c2bf66db92";

    var clientInfo = "Basic b2F1dGhfYW5kcm9pZDpodWltb3V6aGluZW5n";

    function init() {
        $ai_setting_success_div.hide();
        $ai_setting_regist_div.show();

        $.ajaxSetup({
            headers: {clientInfo: clientInfo}
        });

    }

    function wxinit() {
        var title = "全国首创AI人工智能识别坐姿";
        var content = "一款实时监控辅助帮助孩子矫正坐姿的人工智能软件，有效解决青少年驼背近视等问题。能够自动提醒孩子矫正坐姿，并且支持家长实时查看。";
        var image = "/static/component/images/logo.png";
        wx.config({
            appId: $("#appId").val(), // 必填，appID公众号的唯一标识
            timestamp: $("#timestamp").val(), // 必填，生成签名的时间戳
            nonceStr: $("#nonceStr").val(), // 必填，生成签名的随机串
            signature: $("#signature").val(),// 必填，签名，见附录1
            jsApiList: ['updateAppMessageShareData',
                'updateTimelineShareData']
            // 必填，需要使用的JS接口列表，所有JS接口列表见附录2
        });
        wx.error(function (res) {
            console.log(res);
        });
        wx.ready(function () {
            wx.updateAppMessageShareData({
                title: title, // 分享标题
                desc: content, // 分享描述
                link: $("#url").val(), // 分享链接，该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
                imgUrl: image // 分享图标
            });

            wx.updateTimelineShareData({
                title: title, // 分享标题
                link: $("#url").val(), // 分享链接，该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
                imgUrl: image // 分享图标
            });

        });
    }
    wxinit();

    $(".ai-setting-regist-open,.ai-setting-btn-ljsy").on("click", function () {

    });

    $("body").on("click",".lksybtn",function(){
        location.href = "https://a.app.qq.com/o/simple.jsp?pkgname=com.wzapp.sitposture";
    })

    $(".ai-setting-pic-1").on("click",function(){
        weui.dialog({
            title: 'dialog标题',
            content: '<img src="/static/component/images/certificate_one.jpeg">',
            className: 'img-show',
            buttons: [{
                label: ''
            }]
        });
    });
    $(".ai-setting-pic-2").on("click",function(){
        weui.dialog({
            title: 'dialog标题',
            content: '<img src="/static/component/images/certificate_two.jpeg">',
            className: 'img-show',
            buttons: [{
                label: ''
            }]
        });
    });
    $(".ai-setting-pic-3").on("click",function(){
        weui.dialog({
            title: 'dialog标题',
            content: '<img src="/static/component/images/certificate_three.jpeg">',
            className: 'img-show',
            buttons: [{
                label: ''
            }]
        });
    });
    $(".ai-setting-pic-4").on("click",function(){
        weui.dialog({
            title: 'dialog标题',
            content: '<img src="/static/component/images/certificate_four.jpeg">',
            className: 'img-show',
            buttons: [{
                label: ''
            }]
        });
    });
    $(".ai-setting-pic-5").on("click",function(){
        weui.dialog({
            title: 'dialog标题',
            content: '<img src="/static/component/images/certificate_five.jpeg">',
            className: 'img-show',
            buttons: [{
                label: ''
            }]
        });
    });


    init();

    $("#getCodeBtn").on("click", function () {
        var mobile = $("#mobile").val();
        if (!mobile) {
            showError("请输入手机号");
            return;
        }
        $.ajax({
            beforeSend: function () {
                $("#getCodeBtn").attr("disabled", "disabled");
                showLoading("获取验证码中...");
            },
            url: "../authentication/codes/action/fetchByMobile?access_token=" + access_token + "&mobile=" + mobile + "&type=register",
            type: "GET",
            success: function (data) {
                if (data) {
                    beginCountDown();
                }
            },
            error: function (xhr) {
                if (xhr && xhr.responseJSON && xhr.responseJSON.code && xhr.responseJSON.code == 104003) {
                    // $ai_setting_success_div.show();
                    // $ai_setting_regist_div.hide();
                    weui.dialog({
                        title: '',
                        content: '<div>您的手机号已经注册了慧眸智能，<br/>是否立即使用</div><button type="button" class="btn-block btn btn-primary ai-setting-regist-button  lksybtn">立即使用慧眸智能</button>',
                        className: 'already-register',
                        buttons: [{
                            label: ''
                        }]
                    });
                }
                $("#getCodeBtn").attr("disabled", false);
                handlerXhrError(xhr)
            },
            complete: function () {
                hideLoading();
            }
        });
    });

    $("#registerBtn").on("click", function () {
        var mobile = $("#mobile").val();
        var password = $("#password").val();
        var recommendCode = $("#recommendCode").val();
        var code = $("#code").val();
        if (!mobile) {
            showError("请输入手机号");
            return;
        }
        if (!code) {
            showError("请输入验证码");
            return;
        }
        if (!password) {
            showError("请输入密码");
            return;
        }

        $.ajax({
            url: "../userCenter/users/action/register?access_token=" + access_token + "&code=" + code,
            type: "POST",
            beforeSend: function () {
                $("#registerBtn").attr("disabled", "disabled");
                showLoading("注册中...");
            },
            contentType: "application/json",
            data: JSON.stringify({
                mobile: mobile,
                password: md5(password),
                recommendCode: recommendCode,
            }),
            success: function (data) {
                if (data) {
                    $ai_setting_success_div.show();
                    $ai_setting_regist_div.hide();
                }
            },
            error: function (xhr, status, error) {
                handlerXhrError(xhr)
                console.log(xhr);
            },
            complete: function () {
                hideLoading();
            }
        });

    });

    var totalTime = 60000;
    var intervalId = 0;

    function beginCountDown() {
        totalTime = 60000;
        $("#getCodeBtn").attr("disabled", "disabled");
        intervalId = setInterval(function () {
            totalTime = totalTime - 1000;
            if (totalTime == 0) {
                $("#getCodeBtn").html("获取验证码");
                $("#getCodeBtn").attr("disabled", false);
                clearInterval(intervalId);
            } else {
                $("#getCodeBtn").html(totalTime / 1000 + "秒后重发");
            }

        }, 1000);
    }
});


var loading;

function showLoading(msg) {
    loading = weui.loading(msg);
    setTimeout(function () {
        hideLoading();
    }, 10000);
}

function hideLoading() {
    if (loading) {
        loading.hide(function () {
            loading = null;
        });
    }
}

function handlerXhrError(xhr) {
    if (xhr && xhr.responseJSON && xhr.responseJSON.message) {
        showError(xhr.responseJSON.message);
    } else {
        showError("请求出错！");
    }
}


function showError(msg) {
    var dialog = weui.dialog({
        title: '提示',
        content: msg,
        buttons: [{
            label: '我知道了',
            type: 'primary',
            onClick: function () {
                dialog.hide();
            }
        }]
    });
}